package com.kdtech.analyse.outside;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @name 议报
 * @link http://www.yibaochina.com/
 * @author dhy
 */
public class YibaochinaOutsideAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.yibaochina.com/FileView.aspx\\?FileIdq=[0-9]+",
		"http://www.yibaochina.com/new/FileView.aspx\\?FileIdq=[0-9]+"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	public static HashMap<String, String> getLinks(String entryUrl){
		HashMap<String, String> links = new HashMap<String, String>();
		
		UrlMeta meta = CrawlHTML.responseToURL(entryUrl);
		String htmltxt = meta.getHtml();
		Matcher matcher = Pattern.compile("<td style=\"width:50px;\">([0-9]*)</td>").matcher(htmltxt);
		while(matcher.find()){
			String url = "http://www.yibaochina.com/new/FileView.aspx?FileIdq=" + matcher.group(1);
			links.put(url, "");
		}
		
		return links;
	}
	
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title =doc.select("span#ctl00_ContentPlaceHolder1_FormView1_contentLabel b").text();
		
		
		content=HtmlCleaner.getContentHtml(url,doc.select("span#ctl00_ContentPlaceHolder1_FormView1_contentLabel"));
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
